
<template>
	<view>
		<view class="item" 
			v-for="(res, index) in siteList" :key="res.id" 
			v-if="dataBuffer"
		>	
	<u-icon name="close-circle-fill" :size="40" color="#ff5500" :index="res.id" @click="remove(res.id,index)"></u-icon>
			<view class="top" @click="select(index)">
				<view class="name">{{ res.username }}</view>
				<view class="phone">{{ res.userphone }}</view>
				<view class="tag">
					<text class="red" v-if="res.status == 1">默认</text>
				</view>
			</view>
			<view class="bottom" >
				{{res.location+res.useraddress}}
				<u-icon name="edit-pen" :size="40" color="#999999" :index="res.id" @click.native.stop="revise(index)"></u-icon>
			</view>
		</view>
		<view v-else>
			暂无收货地址信息!
		</view>
		<view class="addSite" @tap="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
			</view>
		</view>
	</view>
</template>

<script>
import {addresslist,addressremove}  from '@/api/api'
export default {
	data() {
		return {
			siteList: [
				// {status:1,label:'公司',username:'亚瑟',userphone:13716078900,location:'北京市市辖区朝阳区',useraddress:'五方桥基地'},
				// {status:0,label:'家',username:'鲁班',userphone:13716078900,location:'北京市市辖区朝阳区',useraddress:'五方桥基地'},
				// {status:0,label:'学校',username:'李白',userphone:13716078900,location:'北京市市辖区朝阳区',useraddress:'五方桥基地'},
			],//初始化地址数据
			dataBuffer:true
		};
	},
	onShow() {
		this.getsite()
	},
	onBackPress(option) {
		if(option.from==='backbutton'){
			//
			uni.navigateTo({
				url:'/pages/order/order'
			})
			return true
		}else{
			return false
		}
	},
	methods: {
		// 跳转到新增地址页面
		toAddSite(){
			uni.navigateTo({
				url:'./addSite?id=1'
			})
		},
		//获取地址列表
		getsite(){
			const {uid} = 	uni.getStorageSync("userinof")
			addresslist({uid:uid}).then(res=>{
				console.log(res);
			if(res.data.code ===200){
				if(res.data.list===null){
				this.dataBuffer = false
				return false
				}
				this.siteList = res.data.list
				this.dataBuffer = true
			}else{
		   this.dataBuffer = flase	
			uni.showToast({
				title:'请重新登录',
				icon:'error'
			})
	
			setTimeout(()=>{
				uni.navigateTo({
					url:'/pages/textlogin/textlogin'
				})
			},2000)
				
			}
			})
		},
		revise(index){
	    uni.setStorageSync('addres',this.siteList[index])
			uni.navigateTo({
				url:'/pages/address/addSite'
			})
			
		},
		remove(id,index){
		addressremove({id}).then(res=>{
			if(res.data.code===200){
			uni.showToast({
				title:'删除成功',
				icon:"success"
			})	
		this.siteList.splice(index,1)
			}else{
				uni.showToast({
					title:'请登录',
					icon:"error"
				})	
				setTimeout(()=>{
					uni.navigateTo({
						url:"/pages/textlogin/textlogin"
					})
					
				},2000)
				
			}
		})
		},
		select(index){
		let address = JSON.stringify(this.siteList[index])
			uni.navigateTo({
				url:"/pages/order/order?addr="+address
			})
		
		}
		
	}
};
</script>

<style lang="scss" scoped>
	// 引入样式
	@import  '@/static/css/address.scss';
</style>
